/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sun
 */
public class NewClientKernel implements FriendInfoInterface, ConnectInterface, chatInterface, LoginInterface {

    public static final char LOGIN = 0xF0;
    public static final char LOGOUT = 0xF1;
    public static final char COMMAND = 0xFD;
    public static final char CHATTO = 0xF2;
    public static final char ADDFRIENDS = 0xF3;
    public static final char DELETEFRIENDS = 0xF4;
    public static final char REGIST = 0xF5;
    public static final char ISONLINE = 0xF6;
    public static final char GETFRIENDS = 0xF7;
    public static final char CHAT = 0xF8;
    int port = 54321;
    InetAddress sevAdd;
    String myName, myPass;
    TreeSet<String> friendList, toName;
    Socket socket;
    boolean isConnect = false;
    LinkedList<String> sendQueue;
    BufferedReader in;
    BufferedWriter out;
    boolean isChat = false;

    public NewClientKernel() {
        port = 12345;
        try {
            sevAdd=Inet4Address.getByName("127.0.0.1");
        } catch (UnknownHostException ex) {
            Logger.getLogger(NewClientKernel.class.getName()).log(Level.SEVERE, null, ex);
        }
                sendQueue = new LinkedList<String>();
        friendList = new TreeSet<String>();
        toName = new TreeSet<String>();
    }

    public NewClientKernel(int port, InetAddress sevAdd, String myName) {
        this.port = port;
        this.sevAdd = sevAdd;
        this.myName = myName;
        sendQueue = new LinkedList<String>();
        friendList = new TreeSet<String>();
        toName = new TreeSet<String>();
    }
    //向发送队列添加

    private void addMsg(String Msg) {
        sendQueue.addLast(Msg);
    }
    //链接服务器

    public void textadd(String a) {
    }

    public boolean start() {
        if (!isConnect) {
            try {
                socket = new Socket(sevAdd.getHostAddress(), port);
                isConnect = true;
                InputStream is = null;
                OutputStream os = null;
                is = socket.getInputStream(); 	// 输入流的获取
                os = socket.getOutputStream(); 	// 输出流的获取
                in = new BufferedReader(new InputStreamReader(is));
                out = new BufferedWriter(new OutputStreamWriter(os));
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
        return false;
    }

    public boolean isIsConnect() {
        return isConnect;
    }

    public void setIsConnect(boolean isConnect) {
        this.isConnect = isConnect;
    }

    public String getMyName() {
        return myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }

    @Override
    public boolean addFriend(String friendName) {
        if (friendList.contains(friendName)) {
            return false;
        }
        String ms = NewClientKernel.ADDFRIENDS + "" + friendName;
        addMsg(ms);
        return true;
    }

    @Override
    public boolean delFriend(String friendName) {
        if (!friendList.contains(friendName)) {
            return false;
        }
        String ms = NewClientKernel.DELETEFRIENDS + "" + friendName;
        addMsg(ms);
        return true;
    }

    @Override
    public boolean isOnline(String friendName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String[] getFreinds() {
        try {
            //        return friendList;
            String ms = NewClientKernel.GETFRIENDS + " " + myName;
            out.write(ms);

        } catch (IOException ex) {
            Logger.getLogger(NewClientKernel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public void setConnect(int port, InetAddress sevAdd) {
        this.port = port;
        this.sevAdd = sevAdd;

    }

    @Override
    public boolean chatTo(String[] toName, String msg) {
        String ms = NewClientKernel.CHATTO + "";
        for (String var : toName) {
            ms += var + " ";
        }
        ms+=msg;
        addMsg(ms);
        return true;
    }

    @Override
    public boolean login(String umane, String pass) {
        this.myName = umane;
        this.myPass = pass;
        String ms = NewClientKernel.LOGIN + "" + umane + " " + pass;
        addMsg(ms);
        return true;
    }

    @Override
    public boolean regist(String umane, String pass) {
        this.myName = umane;
        this.myPass = pass;
        String ms = NewClientKernel.REGIST + "" + umane + " " + pass;
        addMsg(ms);
        return true;
    }

    @Override
    public boolean logout() {
        String ms = NewClientKernel.LOGOUT + "" + myName;
        addMsg(ms);
        return true;
    }

    class MsgSender extends Thread {

        @Override
        public void run() {
            while (isConnect) {
                for (Object var : sendQueue.toArray()) {
                    try {
                        out.write(var.toString());
                    } catch (IOException ex) {
                        Logger.getLogger(NewClientKernel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    class rev extends Thread {

        public void propmt(String a) {
            switch (a.charAt(0)) {
                case NewClientKernel.ADDFRIENDS:
                    String[] var = a.substring(1).split(" ");
                    if (var[0].equals("y")) {
                        friendList.add(var[1]);
                    } else {
                    }
                    break;
                case NewClientKernel.REGIST:
                    if (a.charAt(1) == 'y') {
                        isConnect = true;
                    }
                    break;
                case NewClientKernel.GETFRIENDS:
                    for (String v : a.substring(1).split(" ")) {
                        friendList.add(v);
                    }
                    break;
                case NewClientKernel.DELETEFRIENDS:
                    break;
                case NewClientKernel.LOGIN:
                    if (a.charAt(1) == 'y') {
                        isConnect = true;
                    }
                    break;
                case NewClientKernel.LOGOUT:
                    System.exit(0);
                    break;
                case NewClientKernel.CHATTO:
                    if (a.charAt(1) == 'y') {
                        isChat = true;
                    }
                    break;
                case NewClientKernel.CHAT:
                    textadd(a.substring(1));
                    break;

            }
        }

        @Override
        public void run() {
            try {
                String tp = in.readLine();
                System.out.println(tp);
                propmt(tp);
            } catch (IOException ex) {
                Logger.getLogger(NewClientKernel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
